package a;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Byte {
    
    public static void main(String[] args) {
        try {
            //用UTF-8编码
            File input = new File(args[0]);
            File output = new File(args[1]);

            FileInputStream fis = new FileInputStream(input);//文件字节流FileInputStream
            BufferedInputStream bis = new BufferedInputStream(fis);//缓冲输入流BufferedInputStream，单纯缓存，读的更快
            DataInputStream dis = new DataInputStream(bis);//处理流，实现数据输入DataInputStream，readFully

            byte[] buffer = new byte[(int) input.length()];

            dis.readFully(buffer);

            String s = new String(buffer,"GBK");/*常用*/

            dis.close();;
            bis.close();
            fis.close();

            FileOutputStream fos = new FileOutputStream(output);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(s);
            bw.close();
            osw.close();
            fos.close();

            //default encoding 
            // File file = new File(args[0]);
            // int length = (int)file.length();
            // byte[] buffer = new buffer[length];
            // FileInputStream fis = new FileInputStream(file);
            // BufferedInputStream bis = new BufferedInputStream(fis);
            // DataInputStream dis = new DataInputStream(bis);
            // dis.readFully(buffer);
            // dis.close();
            // bis.close();
            // fis.close();
            // String str = new String(buffer,"GBK");
            // StringReader sr = new StringReader(str);
            // BufferedReader br = new BufferedReader(sr);

            //System.out = null;//null是final类型
            
            //FileInputStream fis = new FileInputStream(args[0]);//字节输入流
            //InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
            //BufferedReader br = new BufferedReader(isr);//缓存，而且支持行数据输入

            // FileReader fr = new FileReader(args[0]);//相对路径，这里是默认编码，读其他编码文件会出错
            // /*  计算机处理字符文件必须规定编码方式 GB2312->GBK->GB18030  
            //  *  UTF-8->(1个汉字三个字节，变长编码) UCS
            //  *  java用UNICODE
            //  */ 
            // BufferedReader br = new BufferedReader(fr);

            // char c = '中';
            // String line = null;

            // while((line = br.readLine()) != null ){
            //     System.out.println(line);
            // }

            // br.close();
            // fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

//  javac -d . a.Byte.java  有package a 时用 -d


// class Student implements Serializable{
//     //   序列化会把全部的东西序列化，例如包含的其他类对象 Mobile m
//     private Mobile m;
//     Student(){};
//     Student s = new Student();
//     DataOutputStream dos = new DataOutputStream(new FileOutputStream());
//     ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream());
//     oos.writeObject(oos);
//     ObjectInputStream ois = new ObjectInputStream(new FileInputStream());
//     Student s = (Student) ois.readObject();
// }


/*  异常处理
  异常类的根类 Throwable error子类错误 exception子类异常    
  异常分类 运行异常，检查异常
  java.lang.RuntimeException 运行异常  可避免  编译器不做检查
  检查异常 无法避免

  捕获catch  或  声明throws
  
    



*/

class E{
    public static void main(String[] args) throws FileNotFoundException,IOException,Exception {
        try{
        FileInputStream fis = new FileInputStream(args[0]);

        int r = fis.read();

        }
        catch(FileNotFoundException e){//   多态，FileNotFoundException是
                                       //  IOException派生类，放在前面，放后面java会报错
            e.printStackTrace();
            return;
            //break;
            //continue;
            
        }catch(IOException e){
            throw new Exception(e);
        }
    }
}

/*
 * Exception
 *      String message;
 *      Exception cause;
 * 
 * if(false){}  unreachable statement 报错
 * 
 */

























